﻿(function () {

    var AppLot = function (opts) {

        var resourse;
        var $txtDom = opts.$txtDom;
        var resPath = opts.resPath; //'Content/data/lottery.json'
        var iflag = 0;
        var intervalFlag;

        function start(callback) {

            $.getJSON(resPath, function (res) {

                if (!resourse)
                    resourse = res.data;

                var names = _.where(resourse, { "Used": "0" });
                var _names = _.clone(names);
                var _len = _names.length;
                intervalFlag = setInterval(function () {
                    var i = iflag % _len;
                    $txtDom.html(_names[i].Name);
                    $txtDom.data('id', _names[i].Id)
                    iflag++;
                }, 500);
            });

        }

        function stop(iscontinue, callback) {
            var _id = $txtDom.data('id');
            var obj = _.find(resourse, function (o) { return o.Id == _id; });
            obj.Used = "1";
            clearInterval(intervalFlag);
            if (iscontinue) {
                start();
            } 
        }

        return {
            start: start,
            stop: stop
        }
    }

    if (!window.AppLot)
        window.AppLot = AppLot;

})()